package com.morn.weibo.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.j256.ormlite.field.DatabaseField;

public class Conversation implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3622575060881834507L;

	@DatabaseField(id = true)
	private String otherid;

	@DatabaseField(foreign = true)
	private Message message;

	@DatabaseField
	private Integer msg_num;

	@DatabaseField
	private Boolean new_conv;

	public Conversation() {

	}

	public Conversation(JSONObject json) throws Exception {
		init(json);
	}

	private void init(JSONObject json) throws Exception {
		if (json != null) {
			try {
				message = new Message(json.getJSONObject("dm"));
				otherid = json.getString("otherid");
				msg_num = json.getInt("msg_num");
				new_conv = json.getBoolean("new_conv");
			} catch (JSONException e) {
				throw e;
			}
		}
	}

	public static List<Conversation> getConversationList(JSONArray array)
			throws Exception {
		int size = array.length();
		List<Conversation> list = new ArrayList<Conversation>();
		for (int i = 0; i < size; i++) {
			list.add(new Conversation(array.getJSONObject(i)));
		}
		return list;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getOtherid() {
		return otherid;
	}

	public void setOtherid(String otherid) {
		this.otherid = otherid;
	}

	public Integer getMsg_num() {
		return msg_num;
	}

	public void setMsg_num(Integer msg_num) {
		this.msg_num = msg_num;
	}

	public Boolean getNew_conv() {
		return new_conv;
	}

	public void setNew_conv(Boolean new_conv) {
		this.new_conv = new_conv;
	}

	@Override
	public String toString() {
		return "Conversation [message=" + message + ", otherid=" + otherid
				+ ", msg_num=" + msg_num + ", new_conv=" + new_conv + "]";
	}

}
